//
//  VolumeViewController.m
//  ReadDemo
//
//  Created by YPShao on 2017/11/8.
//  Copyright © 2017年 YPShao. All rights reserved.
//

#import "VolumeViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface VolumeViewController ()

@property (nonatomic, assign) CGFloat initVolumeValue;
@property (nonatomic, assign) BOOL shouldVolumeChange;
@property (nonatomic, strong) UILabel *textLabel;

@end

@implementation VolumeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //进入后台
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(intoBackGround:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    //进入前台
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(returnBackGround:) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    //监听音量调节
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    volumeView.center = CGPointMake(-550,370);//设置中心点，让音量视图不显示在屏幕中
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];
    UISlider *volumeSlider = nil;
    for (UIView *view in [volumeView subviews]){
        if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
            volumeSlider = (UISlider*)view;
            NSLog(@"音量value----->%f",volumeSlider.value);
            self.initVolumeValue = volumeSlider.value;
        } else {
            [view removeFromSuperview];
        }
    }
    
    UILabel *textLabel = [[UILabel  alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];
    [self.view addSubview:textLabel];
    self.textLabel = textLabel;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.shouldVolumeChange = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.shouldVolumeChange = NO;
}

-(void)volumeChanged:(NSNotification *)noti
{
    if (!self.shouldVolumeChange) return;
    float volume = [[[noti userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
    NSLog(@"volumn is %f", volume);
    NSString *str1 = [[noti userInfo]objectForKey:@"AVSystemController_AudioCategoryNotificationParameter"];
    NSString *str2 = [[noti userInfo]objectForKey:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"];
    if (([str1 isEqualToString:@"Audio/Video"] || [str1 isEqualToString:@"Ringtone"]) && ([str2 isEqualToString:@"ExplicitVolumeChange"]))
    {  //这里做你想要的进行的操作
        if (volume > self.initVolumeValue || volume == 1) {
            // 增大
             NSLog(@"按了音量增大了");
            [self pressVolumeUp:YES];
        } else {
            NSLog(@"按了音量减小");
            [self pressVolumeUp:NO];
        }
        self.initVolumeValue = volume;
    }
}

- (void)pressVolumeUp:(BOOL)isUp
{
    if (isUp) {
        self.textLabel.text = @"按了音量增大";
    } else {
        self.textLabel.text = @"按了音量减小了";
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.textLabel.text = nil;
    });

}


-(void)intoBackGround:(NSNotification *)noti
{
    self.shouldVolumeChange = NO;
}

- (void)returnBackGround:(NSNotification *)noti
{
    self.shouldVolumeChange = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
